<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/19
 * Time: 15:45
 */

namespace app\admin\controller;


use app\common\model\SysNotice;
use app\common\model\SysNoticeLog;
use think\Exception;

class Notice extends Admin
{

    /**
     * 配置信息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(){
        $page = input('page',1);
        $limit = input('limit',20);
        $search_word = input('search_word', '', 'trim');
        $param = [];
        if($search_word){ //基础配置
            $param['notice_title|content'] = ['like','%'.$search_word.'%'];
        }
        $model = new SysNotice();
        $count = $model->where($param)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $list = $model->where($param)->page($page,$limit)->order('id desc')->select();
        return json_tab_msg($count, $page, $list);
    }

    /**
     * 新增修改
     * @return \think\response\Json
     */
    public function modify(){
        $data['id']= input('id');
        $data['notice_title'] = input('notice_title','','trim');
        $data['content'] = input('content');
        $data['pic_urls'] = input('pic_urls');
        $data['state'] = 1;
        $data['add_user_id'] = input('opt_user_id');
        $data['add_user_name'] = input('opt_user_name');
        $data['user_type'] = input('user_type');

        $daytime = date('Y-m-d H:i:s');
        $model =new SysNotice();

        if($data['id']){ //修改信息
            $result = $this->validate($data,'Notice.update');
            if ($result !== true) json_return_code('',$result,201);
            // $model->where(['state'=>1])->setField('state',0);
            $data['update_datetime'] = $daytime;
            $model->save($data,array('id'=>$data['id']));
            $msg = '修改成功';
        }else{ //新增信息
            $result = $this->validate($data,'Notice.add');
            if ($result !== true) json_return_code('',$result,201);
            // $model->where(['state'=>1])->setField('state',0);
            $data['add_datetime'] = $daytime;
            $model->insert($data);
            $msg = '添加成功';
        }
        json_return_code($msg);
    }

    public function changeState()
    {
        $id = input('id');
        $state = input('state');
        $model = new SysNotice();
        if (empty($id)){
            json_return_code('','请选择数据!',201);
        }
        /*if($state){
            $model->isUpdate(true)->save(array('state'=>0), array('state'=>1));
        }*/
        $res = $model->save(array('state'=>$state), array('id'=>$id));
        if($res !== false){
            json_return_code('修改成功!');
        }else{
            json_return_code('','修改失败，请重试!',201);
        }

    }
    /**
     * 删除数据
     * @return void
     */
    public function del()
    {
        $id = input('id');
        if(!$id){
            json_return_code('','请选择数据!',201);
        }
        try {
            SysNotice::destroy($id);
            json_return_code( '删除成功!');
        }catch (\Exception $e){
            json_return_code('','删除失败，请重试!'.$e->getMessage(),201);
        }
    }

    public function getUserNotice()
    {
        $model = new SysNotice();
        $info = $model->where(['state'=>1,'user_type'=>['in',[0,1]]])->order('id desc')->find();
        if($info){
            $param['user_id'] = input('opt_user_id');
            $param['user_type'] = 1;
            $param['notice_id'] = $info['id'];
            $logModel = new SysNoticeLog();
            $info['user_state'] = $logModel->where($param)->count();
            if(!$info['user_state']){
                $param['add_datetime'] = date('Y-m-d H:i:s');
                $logModel->insert($param);
            }
        }
        json_return_code($info);
    }
}